package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class UserController  {

    @RequestMapping("/commonParam")
    @ResponseBody
    public String save(String name,int age){

        System.out.println("普通参数从传递name"+name);
        System.out.println("普通参数从传递age"+age);
        return "{'modules':'user save'}";
    }
    //请求参数名与形参名不同
    public String commonParamDifferentName(@RequestParam("name") String username, int age){

        System.out.println("普通参数从传递name"+username);
        System.out.println("普通参数从传递age"+age);
        return "{'modules':'user save'}";

    }
//    pojo参数
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user) {
        System.out.println("pojo参数传递user="+user);
        return "{'modules':'pojo  save'}";
    }

    //数组
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes){
        System.out.println("数组传递参数likes="+ Arrays.toString(likes));
        return "{'modules':'array param'}";
    }
    //集合参数
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> list){
        System.out.println("集合传递参数list="+list.toString());
        return "{'modules':'list param'}";
    }
    //集合参数json格式
    @ResponseBody
    @RequestMapping("/listParamForJson")
    public String listParamForJson( @RequestBody List<String> likes){
        return "{'modules':'list for json param'}";

    }
    @ResponseBody
    @RequestMapping("/arrayParamForJson")
    public String listParamForJson( @RequestBody String[] likes){
        return "{'modules':'array for json param'}";

    }
    @RequestMapping("/pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user){
        System.out.println(user);
        return "{'modules':'pojo for json param'}";
    }
//日期类型参数传递
    @RequestMapping("/dateParam")
    @ResponseBody
    public String dataParam(Date date, @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1){
        System.out.println("参数传递date"+date);
        System.out.println("参数传递date"+date1);

        return "{'module':'data param'}";
    }

}
